package threads;/*
 *@author wenqh
 *@date 2024/4/22 10:12
 *@Description
 */

public class MySynchronized extends Thread{

    static int tacket = 0;

    static Object object =new Object();

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synchronized (object){
                if(tacket>99){
                    break;
                }
                tacket ++;
                System.out.println(Thread.currentThread().getName()+"卖了"+tacket+"张票");
            }

        }
    }

    public static void main(String[] args) {
        MySynchronized mySynchronized = new MySynchronized();
        mySynchronized.setName("窗口1");
        MySynchronized mySynchronized1 = new MySynchronized();
        mySynchronized1.setName("窗口2");
        mySynchronized.run();
        mySynchronized1.run();
    }
}
